{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "reliable-printer",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/most-common-word\n",
    "\n",
    "\n",
    "Runtime: 28 ms, faster than 5.79% of C++ online submissions for Most Common Word.\n",
    "Memory Usage: 18.4 MB, less than 6.26% of C++ online submissions for Most Common Word.\n",
    "\n",
    "\n",
    "\n",
    "```c++\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <string>\n",
    "#include <regex>\n",
    "#include <iostream>\n",
    "#include <map>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "bool compare(pair<string,int> &a, pair<string,int> &b) {\n",
    "    return a.second > b.second;\n",
    "}\n",
    "\n",
    "char myToLower(char c) {\n",
    "    return tolower(c);\n",
    "}\n",
    "\n",
    "class Solution\n",
    "{\n",
    "public:\n",
    "    string mostCommonWord(string paragraph, vector<string> &banned)\n",
    "    {\n",
    "        //6:02\n",
    "        map<string, int> d;\n",
    "\n",
    "        std::regex word_regex(\"(\\\\w+)\");\n",
    "        auto words_begin =\n",
    "            std::sregex_iterator(paragraph.begin(), paragraph.end(), word_regex);\n",
    "        auto words_end = std::sregex_iterator();\n",
    "        for (std::sregex_iterator i = words_begin; i != words_end; ++i)\n",
    "        {\n",
    "            std::smatch match = *i;\n",
    "            std::string match_str = match.str();\n",
    "            transform(match_str.begin(), match_str.end(), match_str.begin(), myToLower);\n",
    "            if (d.find(match_str) == d.end()) {\n",
    "                d.insert(pair<string,int> {match_str, 1});\n",
    "            } else {\n",
    "                d[match_str] += 1;\n",
    "            }\n",
    "        }\n",
    "\n",
    "        vector<pair<string,int>> arr;\n",
    "        for (auto &it: d) {\n",
    "            arr.push_back(it);\n",
    "        }\n",
    "\n",
    "        sort(arr.begin(), arr.end(), compare);\n",
    "\n",
    "        for (auto &it: arr) {\n",
    "            if (find(banned.begin(), banned.end(), it.first) == banned.end()) {\n",
    "                return it.first;\n",
    "            }\n",
    "        }\n",
    "\n",
    "        return \"yingshaoxo\";\n",
    "        //6:36\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "hungry-idaho",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
